<?php
require_once '../kelas/kategori.php';

require_once '../kelas/produk.php';
$fungsi = new fungsi();
$objProduk = new Produk();
$keamanan = new amankanParam();
$dtGet = $keamanan->getMethod();
$dtPost = $keamanan->postMethod();
if (isset($dtGet['on']) && strlen($dtGet['on']) > 0) {
    $on = $dtGet['on'];
    if ($on == 'getForEdit') {
        $dtPost['id_prod'];
        $dtEdit = $objProduk->getForEdit($dtPost['id_prod']);
        $retJ = array('success' => true, 'data' => $dtEdit);
        echo json_encode($retJ);
    } else if ($on == 'aktifasi') {
        $objProduk->update(array('aktif'=>($dtPost['aktif']==1?0:1)),array('id'=>$dtPost['id']));
        $ex = $objProduk->execute();
        echo json_encode($ex);
    } else if ($on == 'simpan') {
    	$gambarAwal = $dtPost['gambar_awal'];
    	unset($dtPost['gambar_awal']);
        if (isset($dtPost['submit'])) {
            unset($dtPost['submit']);
        }
        $dtPost['waktu']=date('Y-m-d H:i:s');
        $idProd = $dtPost['id'];
        unset($dtPost['id']);
        $namaFile = $_FILES['gambar']['name'];
        if ($idProd == ''){
            if (strlen($namaFile) > 0) {
                $ung = $fungsi->unggah('gambar', '../produk');
                if ($ung['success'] == true) {
                    $namaFUnggah = $ung['filename'];
                    $dtPost['gambar'] = $namaFUnggah;
                    $objProduk->create($dtPost);
                    $retJ = $objProduk->execute();
                } else {
                    $retJ = array('success' => false, 'error_msg' => 'Mengunggah Gambar Gagal');
                }
            } else {
                $retJ = array('success' => false, 'error_msg' => 'Tidak Ada Gambar Yang Di Unggah');
            }
        } else {
            if (strlen($namaFile) > 0) {
                $ung = $fungsi->unggah('gambar', '../produk');
                if ($ung['success'] == true) {
                    $namaFUnggah = $ung['filename'];
                    $dtPost['gambar'] = $namaFUnggah;
                    $objProduk->update($dtPost, array('id' => $idProd));
                    $retJ = $objProduk->execute();
                } else {
                    $retJ = array('success' => false, 'error_msg' => 'Mengunggah Gambar Gagal');
                }
            } else {
                if (isset($dtPost['gambar'])) {
                    unset($dtPost['gambar']);
                }
                $objProduk->update($dtPost, array('id' => $idProd));
                $retJ = $objProduk->execute();
            };
        };
        //echo json_encode($retJ);
        $ec = '';
        if ($retJ['success'] == false) {
            $errQ= (ENVIRONMENT=='development')?":".$retJ['error_query']:"" ;
            $ec .= '<script type="text/javascript">alert("' . $retJ['error_msg'].$errQ. '");document.location="' . BASEURLADMIN . '?v=master&p=produk";</script>';
        }
        else{
            $ec .="<script type='text/javascript'>document.location='".BASEURLADMIN."?v=master&p=produk'</script>";
        };
        echo $ec;
    } else {
        echo json_encode(array('success' => false, 'error_msg' => 'Not Set'));
    }
} else {
    echo json_encode(array('success' => false, 'error_msg' => 'Not Allowed For Direct Access'));
}
exit;
//========
$objProduk = new Produk();
$ung = $fungsi->unggah('gambar', '../produk');
if ($ung['success'] == true) {
    $namaFile = $ung['filename'];
    $dtP = $keamanan->postMethod();
    $dtP['gambar'] = $namaFile;
    $objProduk->create();
} else {
    
}